package com.yonyou.pmclouds.billview.plugin;

import com.yonyou.pmclouds.basecom.consts.BillTypeConst;
import com.yonyou.pmclouds.basecom.entity.SuperVO;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.util.CommonFieldConst;
import com.yonyou.pmclouds.basecom.util.StorageFactory;
import com.yonyou.pmclouds.businessobject.util.BusinessObjectConsts;
import com.yonyou.pmclouds.businessobject.util.PrintPreviewObj2Html;
import com.yonyou.pmclouds.outter.enterprise.EnterpriseInfoQuery;
import com.yonyou.pmclouds.outter.user.UserInfoQuery;
import com.yonyou.pmclouds.safetyrectification.acceptrecord.entity.AcceptRecordVO;
import com.yonyou.pmclouds.safetyrectification.entity.SafetyRectificationVO;
import com.yonyou.pmclouds.safetyrectification.rectificationrecord.entity.RectificationRecordVO;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author th
 * @date 2019/11/5 10:01
 */
@Component
public class SafetyRectifyViewPlugin implements IBillViewCustomPlugin {
    @Autowired
    private UserInfoQuery userInfoQuery;
    @Autowired
    private EnterpriseInfoQuery enterpriseInfoQuery;
    @Value("${print.perLineNum}")
    private int perLineNum;
    @Autowired
    private StorageFactory storageFactory;

    @Override
    public Object[] fillListBuziField(Object[] objects) throws BusinessException {
        if (ArrayUtils.isEmpty(objects))
            return objects;

        userInfoQuery.tranUserInfoField(objects, new String[]{CommonFieldConst.CREATOR, SafetyRectificationVO.PK_RECTIFICATIONER},
                new String[]{CommonFieldConst.CREATOR_NAME, SafetyRectificationVO.RECTIFICATIONER_NAME});
        enterpriseInfoQuery.tranEntInfoField(objects, new String[]{SafetyRectificationVO.PK_CONSTRUCTION_UNIT},
                new String[]{SafetyRectificationVO.CONSTRUCT_UNIT_NAME});

        return objects;
    }

    @Override
    public Map<String, Object[]> fillDetailBuziField(SuperVO superVO, HttpServletRequest request) throws BusinessException {
        Map<String, Object[]> result = new HashMap<>();
        if (superVO == null)
            return result;

        SafetyRectificationVO vo = (SafetyRectificationVO) superVO;
        userInfoQuery.tranUserInfoField(new Object[]{vo}, new String[]{CommonFieldConst.CREATOR, SafetyRectificationVO.PK_RECTIFICATIONER},
                new String[]{CommonFieldConst.CREATOR_NAME, SafetyRectificationVO.RECTIFICATIONER_NAME});
        enterpriseInfoQuery.tranEntInfoField(new SuperVO[]{vo}, new String[]{SafetyRectificationVO.PK_CONSTRUCTION_UNIT},
                new String[]{SafetyRectificationVO.CONSTRUCT_UNIT_NAME});
        result.put(BusinessObjectConsts.PM_SAFETY_RECTIFICATION, new Object[]{vo});

        // 整改记录、验收记录
        List<RectificationRecordVO> rectificationRecordList = vo.getRectificationRecordList();
        if (CollectionUtils.isEmpty(rectificationRecordList)) {
            RectificationRecordVO rectificationRecordVO = new RectificationRecordVO();
            rectificationRecordVO.setPkSafetyRectification(vo.getPkSafetyRectification());
            result.put(BusinessObjectConsts.PM_SAFETY_RECTIFICATION_RECORD, new RectificationRecordVO[]{rectificationRecordVO});
        } else {
            PrintPreviewObj2Html obj2Html = new PrintPreviewObj2Html(PrintPreviewObj2Html.BASE_PREFIX,
                    PrintPreviewObj2Html.IMG, perLineNum);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            for (RectificationRecordVO recordVO : rectificationRecordList) {
                String picHtml = obj2Html.urls2Html(storageFactory.getStorageUtil().signUrlForMultiPictures(recordVO.getPictures(), null, request));
                recordVO.setStrRectificationTime(sdf.format(recordVO.getCreationtime()));
                recordVO.setPictureAddress(picHtml);
                // 验收记录
                AcceptRecordVO acceptRecordVO = recordVO.getAcceptRecordVO();
                if (acceptRecordVO != null)
                    recordVO.setAcceptOpinion(acceptRecordVO.getContent());
            }

            result.put(BusinessObjectConsts.PM_SAFETY_RECTIFICATION_RECORD, rectificationRecordList.toArray(new RectificationRecordVO[0]));
        }

        return result;
    }

    @Override
    public String getBillType() {
        return BillTypeConst.SAFETY_RECTIFICATION_BILLTYPE;
    }
}
